package cn.inbot.shopadmin.module.order.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Method;
import java.util.ArrayList;

import cn.inbot.lib.framework.EventManager;
import cn.inbot.lib.framework.PBApplication;
import cn.inbot.lib.framework.annotation.InjectLayout;
import cn.inbot.lib.framework.mvvm.java.PBJVMFragment;
import cn.inbot.lib.log.PBLog;
import cn.inbot.shopadmin.R;
import cn.inbot.shopadmin.module.order.viewmodel.OrderViewModel;

/**
 * Created by liwenrui on 2021/5/18
 * 订单中心页面
 */
@InjectLayout(layoutResId = R.layout.fragment_order)
public class OrderFragment extends PBJVMFragment<OrderViewModel> {

    private Spinner spinnerSelectShop;
    private Toolbar toolbar;
    // 导航布局
    private TabLayout tabLayout;
    // 视图对象
    private ViewPager viewPager;
    public static OrderFragment create() {
        return new OrderFragment();
    }

    /**
     * 创建toolbar右侧的选择按钮
     * @param menu
     * @param inflater
     */
    @Override
    public void onCreateOptionsMenu(@NonNull @NotNull Menu menu, @NonNull @NotNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        // 加载menu文件
        inflater.inflate(R.menu.menu_overflow, menu);
//        显示图标
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                }
            }
        }
    }

    // 监听右侧选项，添加监听事件
    @Override
    public boolean onOptionsItemSelected(@NonNull @NotNull MenuItem item) {
        PBLog.INSTANCE.d("点击菜单" + item.getItemId());
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        EventManager.INSTANCE.register(this);
        spinnerSelectShop = getContentView().findViewById(R.id.spinner_select_shop);


//        显示右侧菜单
        setHasOptionsMenu(true);

        toolbar = getContentView().findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        String[] arr = {"PadBot无人商店", "PadBot无人商店-琶洲", "PadBot无人商店-客村", "PadBot无人商店-华港22", "PadBot无人商店-琶洲11", "PadBot无人商店"};

        // R.layout.item_select_shop设置的是文字样式
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(PBApplication.appContext, R.layout.item_select_shop, arr);
        // 设定下拉item的样式
        arrayAdapter.setDropDownViewResource(R.layout.item_select_shop_dropdown);

        spinnerSelectShop.setAdapter(arrayAdapter);
        spinnerSelectShop.setOnItemSelectedListener(new ShopSelectedListener());

        // 切换栏viewpage内容
        tabLayout = getContentView().findViewById(R.id.tab_layout);
        viewPager = getContentView().findViewById(R.id.view_pager);
        // viewpage中不同加载不同的fragment
        ArrayList aList = new ArrayList<View>();
        LayoutInflater li = getLayoutInflater();
        aList.add(li.inflate(R.layout.fragment_order_incomplete, null, false));
        aList.add(li.inflate(R.layout.fragment_order_complete, null, false));
        aList.add(li.inflate(R.layout.fragment_order_all, null, false));
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(aList);
        viewPager.setAdapter(myPagerAdapter);

        // 对TabLayout进行初始化
        initTab();
        // 将其TabLayout和viewpage进行联动
        tabLayout.setupWithViewPager(viewPager);
        // 让选择框进行铺满屏幕宽度
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    }

    /**
     * tab的初始化，
     */
    private void initTab(){
        ArrayList<String> tabList = new ArrayList<>();
        tabList.add("未完成订单");
        tabList.add("已完成订单");
        tabList.add("全部订单");
    }


    class ShopSelectedListener implements AdapterView.OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            PBLog.INSTANCE.d("点击");
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        immersiveStatusBar(R.color.colorPrimary, true);
        setupToolbar(R.id.toolbar, false, true).setTitle("");
    }

    @Override
    public void onDestroy() {
        EventManager.INSTANCE.unregister(this);
        super.onDestroy();
    }


    /**
     * viewPage的适配器，将其中内容进行展示
     */
    public static class MyPagerAdapter extends PagerAdapter {

        // 存放View的list
        private ArrayList<View> viewLists;
        private ArrayList<String> tabList;

        public MyPagerAdapter() {

        }

        public MyPagerAdapter(ArrayList<View> viewLists) {
            super();
            this.viewLists = viewLists;
            tabList = new ArrayList<>();
            tabList.add("未完成订单");
            tabList.add("已完成订单");
            tabList.add("全部订单");
        }

        @Override
        public int getCount() {
            return viewLists.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object o) {
            return view == o;
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewLists.get(position));
            return viewLists.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //会导致内存的溢出，所以将其注释掉
//            super.destroyItem(container, position, object);
            container.removeView(viewLists.get(position));
        }

        @androidx.annotation.Nullable
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabList.get(position);
        }
    }

}
